//
//  SQSocketManager.m
//  markmap
//
//  Created by 甘世清 on 2024/5/26.
//

#import "SQSocketManager.h"
#import "IMMessageViewController.h"
@implementation SQSocketManager

//处理心跳的问题
+(void)SQ_SocketManage{
    NSString *url = [NSString stringWithFormat:@"wss://saliao.qingtao.fun/saliaows/ws?client=a&token=%@",[UserModel share].token];
    if (![[UserModel share].token isNotEmpty]) {
        return;
    }
    [[SQWebSocket shareManager] SQ_open:url connect:^{
        DTLog(@"链接成功---%@",[NSString SQ_currentTime]);
    } receive:^(id  _Nonnull message, SQSocketReceiveType type) {
        [SQSocketManager SQ_message:message];
    } failure:^(NSError * _Nonnull error) {
        DTLog(@"---失败了---%@",error);
    }];
}
+(void)SQ_message:(NSString*)message{
    NSString * string = [NSString stringWithFormat:@"%@",message];
    NSDictionary * dic = [[NSDictionary alloc]initWithDictionary:[string mj_JSONObject]];
    DTLog(@"接收的消息 ---%@",dic);
    NSString * order = [NSString stringWithFormat:@"%@",[dic objectForKey:@"order"]];
    BOOL  isAck = [dic  objectForKey:@"isAck"];
    if (![order isNotEmpty]) {
        return;
    }
    NSString * roomID = [NSString stringWithFormat:@"%@",[dic objectForKey:@"roomId"]];
    if ([order isEqualToString:@"0"]) {
    /**
     * 心跳
     */
    } else if ([order isEqualToString:@"100"]) {
    /**
     * 创建房间
     */
        NSMutableDictionary * data = [[NSMutableDictionary alloc]init];
        if (isAck) {
            [data setObject:@"201" forKey:@"order"];
            [[SQWebSocket shareManager]SQ_send:[data mj_JSONString] isSend:^(BOOL isSend) {
            }];
        }
       //创建模型
        IMListModel * model = [IMListModel new];
        model.roomId = roomID;
        model.content = @"";
        model.time = [NSString stringWithFormat:@"%@",[dic objectForKey:@"time"]];
        model.noReadCount = 0;
        model.msgType = 0;
      
        NSString * extString = [NSString stringWithFormat:@"%@",[dic objectForKey:@"ext"]];
        if ([extString isNotEmpty]) {
            NSDictionary * ext = [[NSDictionary alloc]initWithDictionary:[extString mj_JSONObject]];
            model.peopleId = [NSString stringWithFormat:@"%@",[ext objectForKey:@"uid"]];
            model.name = [NSString stringWithFormat:@"%@",[ext objectForKey:@"name"]];
            model.portrait = [NSString stringWithFormat:@"%@",[ext objectForKey:@"portrait"]];
            
            //再跳转到im会话页面
            IMMessageViewController * vc = [[IMMessageViewController alloc]init];
            vc.model = model;
            [[Manager SQ_getCurrentViewController].navigationController pushViewController:vc animated:YES];
            [SQFMDBManager SQ_addListIM:model];//将这个模型存储
        }else{
            [SQFMDBManager SQ_addListIM:model];//将这个模型存储
            //再根据房间号请求房间内的用户
            [data setObject:@"108" forKey:@"order"];
            [data setObject:roomID forKey:@"roomId"];
            [[SQWebSocket shareManager]SQ_send:[data mj_JSONString] isSend:^(BOOL isSend) {
                
            }];
        }
        
    }else if ([order isEqualToString:@"101"]) {
    /**
     * 删除房间
     */
    }else if ([order isEqualToString:@"108"]) {
    /**
     * 房间所有用户
     */
        NSArray * array = [[NSArray alloc]initWithArray:[dic objectForKey:@"content"]];
        IMListModel * model = [SQFMDBManager SQ_roomIDGetIMListModel:roomID];
        if ([model.roomId isNotEmpty]) {
            for (NSDictionary * info in array) {
                NSString * userid = [NSString stringWithFormat:@"%@",[info objectForKey:@"id"]];
                if (![userid isEqualToString:[UserModel share].userid]) {
                    model.peopleId = userid;
                    model.name = [NSString stringWithFormat:@"%@",[info objectForKey:@"name"]];
                    model.portrait = [NSString stringWithFormat:@"%@",[info objectForKey:@"portrait"]];
                    [SQFMDBManager SQ_addListIM:model];
                    break;
                }
            }
            //再跳转到im会话页面
            IMMessageViewController * vc = [[IMMessageViewController alloc]init];
            vc.model = model;
            [[Manager SQ_getCurrentViewController].navigationController pushViewController:vc animated:YES];
        }
        
    }else if ([order isEqualToString:@"109"]) {
    /**
     * 用户详情
     */
        
    }else if ([order isEqualToString:@"200"]) {
    /**
     * 发送消息,接收
     */
        //转化模型
        IMMessageModel * model = [IMMessageModel mj_objectWithKeyValues:dic];
        BOOL ismodel = [SQFMDBManager SQ_messageModel:model];
        if (ismodel) {//如果该im存在,就直接通知并不后续操作
            NSMutableDictionary * data = [[NSMutableDictionary alloc]init];
            [data setObject:@"201" forKey:@"order"]; //告知服务器我收到了
            [[SQWebSocket shareManager]SQ_send:[data mj_JSONString] isSend:^(BOOL isSend){
            }];
            return;
        }
        if (model.msgType == 1) {  // 图片 数据处理
            NSString * ext = [dic objectForKey:@"ext"];
            if ([ext isNotEmpty]) {
                NSDictionary * extDic = [ext mj_JSONObject];
                NSString * h = [NSString stringWithFormat:@"%@",[extDic objectForKey:@"imageH"]];
                if ([h isNotEmpty]) {
                    model.imageH = [h floatValue];
                }
                NSString * w = [NSString stringWithFormat:@"%@",[extDic objectForKey:@"imageW"]];
                if ([w isNotEmpty]) {
                    model.imageW = [w floatValue];
                }
            }
        }
        
        bool isok = [SQFMDBManager SQ_addListIMMessage:model]; //存储
        if (isAck && isok) {
            NSMutableDictionary * data = [[NSMutableDictionary alloc]init];
            [data setObject:@"201" forKey:@"order"]; //告知服务器我收到了
            [[SQWebSocket shareManager]SQ_send:[data mj_JSONString] isSend:^(BOOL isSend){
                
            }];
        }
    }else if ([order isEqualToString:@"201"]) {
    /**
     * 消息确认
     */
    }else if ([order isEqualToString:@"204"]) {
    /**
     * 读取所有
     */
    }else{
        NSMutableDictionary * data = [[NSMutableDictionary alloc]init];
        [data setObject:@"201" forKey:@"order"]; //告知服务器我收到了
        [[SQWebSocket shareManager]SQ_send:[data mj_JSONString] isSend:^(BOOL isSend){
        }];
    }
}

@end
